package gui;

import domain.player.Player;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import main.AppConstants;

/**
 * WinnerDialog is shown when a player has one the game.
 */
public class WinnerDialog extends JDialog {

	private Player winner;
	private Container pane;

	public WinnerDialog(Player winner) {
		this.winner = winner;
		if (winner != null) {
			createDialog();
		}
	}

	public void createDialog() {
		setTitle(AppConstants.WINNER_DIALOG_TITLE);
		
		pane = getContentPane();
		
		addWinnerComponents();
		addPlayerData();
		addOKButton();

		setModal(true);
		pack();
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
	}
	
	/** Adds the winner label */
	public void addWinnerComponents() {
		JPanel winnerPanel = new JPanel();
		
		JLabel winnerLabel = new JLabel("The Winner is");
		winnerLabel.setFont(new Font(Font.DIALOG, Font.BOLD, 20));
		winnerLabel.setAlignmentX(winnerLabel.CENTER_ALIGNMENT);
		
		winnerPanel.add(winnerLabel);
		pane.add(winnerPanel, BorderLayout.NORTH);
	}
	
	/** Adds the player data */
	public void addPlayerData() {
		JPanel playerPanel = new JPanel(new FlowLayout());
		
		// Icon:
		JLabel disc = new JLabel();
		disc.setIcon(new DiscIcon(winner.getDiscColor(), 0.5));
		playerPanel.add(disc);
		
		// Player name:
		JLabel playerLabel = new JLabel(winner.getName());
		playerLabel.setFont(new Font(Font.DIALOG, Font.BOLD, 32));
		playerPanel.add(playerLabel);
		
		pane.add(playerPanel, BorderLayout.CENTER);
	}

	/** Add OK button to close the winner dialog */
	private void addOKButton() {
		JButton jBtnOK = new JButton("OK");
		jBtnOK.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
			
		});
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(AppConstants.COLOR_PANEL_BACKGROUND);
		buttonPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
				AppConstants.COLOR_PANEL_BORDER));
		
		buttonPanel.add(jBtnOK);
		pane.add(buttonPanel, BorderLayout.SOUTH);
	}
	
}
